﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommandPattern
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 1 直接new对象方式
            Document document = new Document();
            /* document.Read();
             document.Write();*/

            /* Console.WriteLine("***************************************");
             // 2  让用户输入，根据用户的输入来执行方法
             while (true)
             {
                 string input = Console.ReadLine();
                 if (input == "r")
                     document.Read();
                 if (input == "w")
                     document.Write();
                 else
                     Console.WriteLine("wrong reInput");
             }*/

            // 3 让用户输入，根据用户的输入来执行方法
            /*  while (true)
              {
                  string input = Console.ReadLine();
                  var config = ConfigurationManager.AppSettings[input];
                  if (config == "read")
                      document.Read();
                  if (config == "write")
                      document.Write();
                  else
                      Console.WriteLine("wrong reInput");
              }*/

            /*// 4 初级使用命令模式
            while (true)
            {
                string input = Console.ReadLine();
                var config = ConfigurationManager.AppSettings[input];
                if (string.IsNullOrEmpty(config))
                {
                    Console.WriteLine("输入错误，请重新输入");
                    continue;
                }
                var assm = config.Split(',');
                var assembly = assm[1];
                var type = assm[0];
                BaseCommand command = (BaseCommand)Activator.CreateInstance(assembly, type).Unwrap();
                command.SetDocument(document);
                command.Execute();
            }*/

            // 5 中级使用命令模式
            while (true)
            {
                string input = Console.ReadLine();
                var config = ConfigurationManager.AppSettings[input];
                if (string.IsNullOrEmpty(config))
                {
                    Console.WriteLine("输入错误，请重新输入");
                    continue;
                }
                var assm = config.Split(',');
                var assembly = assm[1];
                var type = assm[0];
                BaseCommand command = (BaseCommand)Activator.CreateInstance(assembly, type).Unwrap();
                command.SetDocument(document);
                IExecuteCommand executeCommand = new ExecuteCommand2();
                command.SetExecuteCommand(executeCommand);
                command.Execute();
            }




        }
    }
}
